<?php

final class Kzend_DbUtils
{
    public static function getStdClass($result, $returnColumns = null, $omitColumns = null)
    {
        if (!$result) {
            return false;
        }
        if ($result instanceof Zend_Db_Table_Row_Abstract) {
            return self::_arrayToStdClass($result->toArray(), $returnColumns, $omitColumns);
        } else if ($result instanceof Zend_Db_Table_Rowset_Abstract) {
            $r = array();
            if ($result->count() > 0) {
                foreach ($result as $row) {
                    $std = self::_arrayToStdClass($row->toArray(), $returnColumns, $omitColumns);
                    array_push($r, $std);
                }
            }
            return $r;
        } else if (is_array($result)) {
            return self::_arrayToStdClass($result, $returnColumns, $omitColumns);
        } else {
            return false;
        }
    }

    private static function _arrayToStdClass($resultArray, $returnColumns = null, $omitColumns = null)
    {
        $returnObject = new stdClass();
        if (null !== $returnColumns) {

            $availableColumns = array_keys($resultArray);
            foreach ((array) $returnColumns as $returnColumn) {
                if (in_array($returnColumn, $availableColumns)) {
                    $returnObject->{$returnColumn} = $resultArray[$returnColumn];
                }
            }
            return $returnObject;

        } elseif (null !== $omitColumns) {

            $omitColumns = (array) $omitColumns;
            foreach ($resultArray as $resultColumn => $resultValue) {
                if (!in_array($resultColumn, $omitColumns)) {
                    $returnObject->{$resultColumn} = $resultValue;
                }
            }
            return $returnObject;

        } else {

            foreach ($resultArray as $resultColumn => $resultValue) {
                $returnObject->{$resultColumn} = $resultValue;
            }
            return $returnObject;
        }
    }

    public static function beginTransaction()
    {
        Zend_Db_Table::getDefaultAdapter()->beginTransaction();
    }

    public static function commit()
    {
        Zend_Db_Table::getDefaultAdapter()->commit();
    }

    public static function rollBack()
    {
        Zend_Db_Table::getDefaultAdapter()->rollBack();
    }
}
